package com.feihao.zhou.test1;

import com.feihao.zhou.bean.*;
import com.feihao.zhou.mapper.ManyToManyMapper;
import com.feihao.zhou.mapper.OneToManyMapper;
import com.feihao.zhou.mapper.OneToOneMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void test1() throws IOException {
        //1.加载核心配置文件：
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        //2.获取sqlsession工厂对象：
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //通过工厂对象，获取sqlsession对象：
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行映射配置文件中的sql语句，接收结果：
        OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);
        List<Card> all = mapper.findAll();

        //遍历打印：
        for (Card card : all) {
            System.out.println(card.getId() + "," + card.getNumber());
            Person person = card.getPerson();
            System.out.println(person);
        }

        //施放资源：
        is.close();
        sqlSession.close();

    }

    @Test
    public void test2() {
        try {


            //1.加载核心配置文件：
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

            //2.获取sqlsession工厂对象：
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //通过工厂对象，获取sqlsession对象：
            SqlSession sqlSession = sqlSessionFactory.openSession();

            //执行映射配置文件中的sql语句，接收结果：
            OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);
            List<Classes> classes = mapper.selectAllClasses();

            //遍历打印：
            for (Classes aClass : classes) {
                System.out.println(aClass.getId() + "," + aClass.getName());
                List<Student> students = aClass.getStudents();
                System.out.println(students);
            }

            //施放资源：
            is.close();
            sqlSession.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() {
        try {


            //1.加载核心配置文件：
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

            //2.获取sqlsession工厂对象：
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

            //通过工厂对象，获取sqlsession对象：
            SqlSession sqlSession = sqlSessionFactory.openSession();

            //执行映射配置文件中的sql语句，接收结果：
            ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class);
            List<Student> list = mapper.selectAll();


            //遍历打印：
            for (Student student : list) {
                System.out.println(student);
                List<Course> courses = student.getCourses();
                for (Course cours : courses) {
                    System.out.println(cours);
                }
            }

            //施放资源：
            is.close();
            sqlSession.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
